嗨,我是 Fly,用 Ruby 寫 Chatbot 並挑戰30天分享心得
為確保不會沒靈感
每日含 Ruby 主題文章增加內容
https://github.com/leo424y/clean-code-ruby
Good:
class Car
  attr_accessor :make, :model, :color
  def initialize(make, model, color)
    @make = make
    @model = model
    @color = color
  end
  def save
    # Save object...
  end
end
car = Car.new('Ford', 'F-150', 'red')
car.color = 'pink'
car.save
Bad:
class Employee
  def initialize(name, email)
    @name = name
    @email = email
  end
  # ...
end
# Bad because Employees "have" tax data. EmployeeTaxData is not a type of Employee
class EmployeeTaxData < Employee
  def initialize(ssn, salary)
    super()
    @ssn = ssn
    @salary = salary
  end
  # ...
end
Good:
class EmployeeTaxData
  def initialize(ssn, salary)
    @ssn = ssn
    @salary = salary
  end
  # ...
end
class Employee
  def initialize(name, email)
    @name = name
    @email = email
  end
  def set_tax_data(ssn, salary)
    @tax_data = EmployeeTaxData.new(ssn, salary)
  end
  # ...
end